Shaders best practices

In OpenGL ES 2.0 and later, where programmable pipeline is available, your application spends considerable amount of time executing shader programs. Especially with mobile and embedded hardware, shaders can be the source of many problems including performance bottlenecks and graphical artifacts. When used properly, shaders can help you produce amazing graphical quality and performance by distributing computation between the CPU and the GPU. You have to decide whether to perform computations in the fragment or the vertex shader.

A vertex shader of a shader program is run once for each vertex while its fragment shader is run once for each pixel. Usually there are an order of magnitude more pixels than vertices, so the best practice is to calculate as much as possible in vertex shader and use the results in the fragment shader.

Some of the most common shader bottlenecks are:

Kanzi Studio provides a template for the low precision (ES2Low) and high precision (ES2High) shader profiles. The main difference between these templates is that most computation, including applying lights, is done for high precision in the fragment shader (per pixel) and for low precision in the vertex shader. Depending on the graphics hardware, and the complexity of the scene, the effect in performance can be dramatic.

For mobile and embedded devices, use low precision for all variables that range [-2..2]. You can optimize performance by decreasing the precision for the used uniform and varying attributes in shaders. Precision in shaders is specific to GLES.

Shaders are material-specific. You can modify shaders directly in Kanzi Studio. See Editing shaders.

Best practices for using shaders in your Kanzi application are:

See also

Reducing shader switches

Using binary shaders

Optimizing fragment shaders

Setting how Kanzi Engine handles unused resources

Loading resources in parallel

Editing shaders

Preventing overdraw with the Sorting Filter

Measuring the performance of your application

Best practices